<template>
  <div class="app-container">
    <el-form :inline="true" label-width="68px">
      
      <el-form-item label="会员名称">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入会员名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="queryParams.defaultStatus" placeholder="会员状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="postList">
      <el-table-column label="会员名称" align="center" prop="name" />
      <el-table-column label="基准积分" align="center" prop="growthPoint" />
      <el-table-column label="免邮标准" align="center" prop="freeFreightPoint" />
      <el-table-column label="评价成长值" align="center" prop="commentGrowthPoint"  />
      <el-table-column label="默认等级" align="center" prop="defaultStatus" :formatter="statusFormat" />
      <el-table-column label="免邮特权" align="center" prop="priviledgeFreeFreight" :formatter="statusFormat"/>
      <el-table-column label="签到特权" align="center" prop="priviledgeSignIn" :formatter="statusFormat"/>
      <el-table-column label="评论特权" align="center" prop="priviledgeComment" :formatter="statusFormat"/>
      <el-table-column label="专享特权" align="center" prop="priviledgePromotion" :formatter="statusFormat"/>
      <el-table-column label="价格特权" align="center" prop="priviledgeMemberPrice" :formatter="statusFormat"/>
      <el-table-column label="生日特权" align="center" prop="priviledgeBirthday" :formatter="statusFormat"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button 
            size="mini" 
            type="text" 
            icon="el-icon-edit" 
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            layout="total, sizes,prev, pager, next,jumper"
            :page-size="queryParams.pageSize"
            :page-sizes="[5,10,15]"
            :current-page.sync="queryParams.pageNum"
            :total="total">
          </el-pagination>
    </div>

    <!-- 添加或修改会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="会员名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入会员名称" />
        </el-form-item>
        <el-form-item label="基准积分" prop="growthPoint">
          <el-input-number v-model="form.growthPoint"  placeholder="请输入基准积分" />
        </el-form-item>
        <el-form-item label="免邮标准" prop="freeFreightPoint">
          <el-input-number v-model="form.freeFreightPoint"  placeholder="请输入免邮标准金额" />
        </el-form-item>
        <el-form-item label="评价成长值" prop="commentGrowthPoint">
          <el-input-number v-model="form.commentGrowthPoint"  placeholder="请输入每次评价获取的成长值" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" />
        </el-form-item>
        <el-form-item label="默认等级" prop="defaultStatus">
          <el-radio-group v-model="form.defaultStatus">
             <el-radio :label="0" >不是</el-radio>
             <el-radio :label="1" >是</el-radio>
          </el-radio-group>
        </el-form-item>
         <el-form-item label="是否有免邮特权" prop="priviledgeFreeFreight">
          <el-radio-group v-model="form.priviledgeFreeFreight">
            <el-radio :label="0" >不是</el-radio>
            <el-radio :label="1" >是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有签到特权" prop="priviledgeSignIn">
          <el-radio-group v-model="form.priviledgeSignIn">
            <el-radio :label="0" >不是</el-radio>
            <el-radio :label="1" >是</el-radio>
           </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有评论获奖励特权" prop="priviledgePromotion">
          <el-radio-group v-model="form.priviledgePromotion">
            <el-radio :label="0" >不是</el-radio>
            <el-radio :label="1" >是</el-radio>
           </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有会员价格特权" prop="priviledgeMemberPrice">
          <el-radio-group v-model="form.priviledgeMemberPrice">
            <el-radio :label="0" >不是</el-radio>
            <el-radio :label="1" >是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有生日特权" prop="priviledgeBirthday">
          <el-radio-group v-model="form.priviledgeBirthday">
            <el-radio :label="0" >不是</el-radio>
            <el-radio :label="1" >是</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注说明" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入备注说明" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listVip, getVip, delVip, addVip, updateVip } from "@/api/memberLevel";

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 会员表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [{dictValue:"0",dictLabel:"不是"},{dictValue:"1",dictLabel:"是"}],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        defaultStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "会员名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listVip(this.queryParams).then(response => {
        this.postList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      var v = row[column.property];
      return this.selectDictLabel(this.statusOptions, v);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        growthPoint: undefined,
        defaultStatus:"0",
        freeFreightPoint:0,
        commentGrowthPoint:0,
        sort:0,
        priviledgeFreeFreight:"0",
        priviledgeSignIn:"0",
        priviledgeComment:"0",
        priviledgePromotion:"0",
        priviledgeMemberPrice:"0",
        priviledgeBirthday:"0",
        note: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getVip(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateVip(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addVip(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    handleSizeChange(val) {
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = val;
        this.getList();
      },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除会员名称为"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delVip(row.id);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>